//
//  LCZReadCell.m
//  PersonalNews
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZReadCell.h"
#import "LCZReadDetailCell.h"
#import "ReadRecommend.h"
#import "LCZNextController.h"
#import "LCZPicCell.h"
#import "LCZOneCell.h"
#import "LCZReadViewController.h"

// 阅读网址
#define YUEDU @"http://c.3g.163.com/recommend/getSubDocPic?passport=&devId=1019620C-F598-49E2-8367-EE99842C40BA&size=20&version=5.3.4&spever=false&net=wifi&lat=38.883477&lon=121.544361&from=yuedu"

static NSString *const firstCell = @"first";
@implementation LCZReadCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.readTableView = [[LCZBaseTableView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
        [self.contentView addSubview:self.readTableView];
        self.readTableView.delegate = self;
        self.readTableView.dataSource = self;
        
        // 初始化数组
        self.recommendArr = [NSMutableArray array];
    }
    return self;
}

// 重写setter方法
- (void)setScrollNum:(NSInteger)scrollNum{
    _scrollNum = scrollNum;
    NSLog(@"%ld", self.scrollNum);
    
    // 请求数据
    [self create];
    
    self.readTableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        [self create];
        [self.readTableView.mj_header endRefreshing];
    }];
    
    // 上拉加载数据
    self.readTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self create];
        [self.readTableView.mj_footer endRefreshing];
        
    }];
   
    [self.readTableView reloadData];
}

// 第一个tableView数据和加载数据
- (void)create{
    
        self.strURL = YUEDU;
    
        [LCZNetWorkTools getdataWithString:self.strURL block:^(id block) {
            
            NSDictionary *dic = block;
            NSArray *array = dic[@"推荐"];
            for (NSDictionary *temp in array) {
                ReadRecommend *read = [[ReadRecommend alloc] init];
                [read setValuesForKeysWithDictionary:temp];
                [self.recommendArr addObject:read];
            }

                [self.readTableView reloadData];
        }];

}

#pragma mark 协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        ReadRecommend *news = self.recommendArr[indexPath.row];
        NSMutableArray *array = news.imgnewextra;
        if (array.count != 0) {
            LCZPicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threeReuse"];
            if (!cell) {
                cell = [[LCZPicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"threeReuse"];
            }
            [cell.imgsrcImageView2 sd_setImageWithURL:[NSURL URLWithString:news.imgsrc] placeholderImage:[UIImage imageNamed:@"0.jpg"]];
            cell.titleLabel.text = news.title;
            //    无色
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            NSDictionary *dic = news.imgnewextra[0];
            news.imgsrc1 = dic[@"imgsrc"];
            [cell.imgsrcImageView sd_setImageWithURL:[NSURL URLWithString:news.imgsrc1]];
            NSDictionary *dic1 = news.imgnewextra[1];
            news.imgsrc2 = dic1[@"imgsrc"];
            [cell.imgsrcImageView1 sd_setImageWithURL:[NSURL URLWithString:news.imgsrc2]];
            
            return cell;
        }
        else {
            
            LCZOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oneReuse"];
            if (!cell) {
                cell = [[LCZOneCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"oneReuse"];
            }
            cell.titleLabel.numberOfLines = 0;
            cell.titleLabel.text = news.title;
            [cell.titleLabel sizeToFit];
            cell.replyCountLabel.text = news.source;
            [cell.imgsrcImageView sd_setImageWithURL:[NSURL URLWithString:news.imgsrc] placeholderImage:[UIImage imageNamed:@"0.jpg"]];
            
            return cell;
        }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.recommendArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        ReadRecommend *read = self.recommendArr[indexPath.row];
        // push到下一页的时候传值
        [self.delegate push:read.replyid :read];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        ReadRecommend *read = self.recommendArr[indexPath.row];
        if (read.imgnewextra != nil) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width * 0.7, 10)];
            label.numberOfLines = 0;
            label.text = read.title;
            [label sizeToFit];
            CGFloat i = label.frame.size.height;
            NSLog(@"%g", i);
            return i + 110 + 4;
        }else{
            
            return 120 + 4;
        }
    
}

@end
